<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book django项目 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>视图集ViewSet | django项目</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C04-View/Routers.html" />
    
    
    <link rel="prev" href="../C04-View/ViewIntroduction.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="4.4"
        data-chapter-title="视图集ViewSet"
        data-filepath="C04-View/ViewSet.md"
        data-basepath=".."
        data-revision="Sun Nov 18 2018 07:16:17 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="C01-IntroduceToDRF/index.html">
            
                
                    <a href="../C01-IntroduceToDRF/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        引入DjangoRESTframework
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="C01-IntroduceToDRF/SeparatedFrontendAndBackend.html">
            
                
                    <a href="../C01-IntroduceToDRF/SeparatedFrontendAndBackend.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        Web应用模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="C01-IntroduceToDRF/IntroduceToREST.html">
            
                
                    <a href="../C01-IntroduceToDRF/IntroduceToREST.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        认识RESTful
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="C01-IntroduceToDRF/HowToDesignREST.html">
            
                
                    <a href="../C01-IntroduceToDRF/HowToDesignREST.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        RESTful设计方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="C01-IntroduceToDRF/DevelopRESTAPIWithDjango.html">
            
                
                    <a href="../C01-IntroduceToDRF/DevelopRESTAPIWithDjango.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        使用Django开发REST接口
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="C01-IntroduceToDRF/CoreTaskToDevelopRESTAPI.html">
            
                
                    <a href="../C01-IntroduceToDRF/CoreTaskToDevelopRESTAPI.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        明确REST接口开发的核心任务
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="C01-IntroduceToDRF/AboutDRF.html">
            
                
                    <a href="../C01-IntroduceToDRF/AboutDRF.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        DjangoRESTframework简介
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C02-DRFProject/index.html">
            
                
                    <a href="../C02-DRFProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        DRF工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C02-DRFProject/InstallAndConfig.html">
            
                
                    <a href="../C02-DRFProject/InstallAndConfig.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装与配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C02-DRFProject/TheFirstDRFProgram.html">
            
                
                    <a href="../C02-DRFProject/TheFirstDRFProgram.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        见识DRF的魅力
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C03-Serializer/index.html">
            
                
                    <a href="../C03-Serializer/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Serializer序列化器
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C03-Serializer/Declaring.html">
            
                
                    <a href="../C03-Serializer/Declaring.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        定义Serializer
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C03-Serializer/Serializing.html">
            
                
                    <a href="../C03-Serializer/Serializing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        序列化使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C03-Serializer/Deserializing.html">
            
                
                    <a href="../C03-Serializer/Deserializing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        反序列化使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="C03-Serializer/ModelSerializer.html">
            
                
                    <a href="../C03-Serializer/ModelSerializer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        模型类序列化器ModelSerializer
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C04-View/index.html">
            
                
                    <a href="../C04-View/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C04-View/RequestAndResponse.html">
            
                
                    <a href="../C04-View/RequestAndResponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        Request与Response
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C04-View/View.html">
            
                
                    <a href="../C04-View/View.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        视图概览
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C04-View/ViewIntroduction.html">
            
                
                    <a href="../C04-View/ViewIntroduction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        视图说明
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="4.4" data-path="C04-View/ViewSet.html">
            
                
                    <a href="../C04-View/ViewSet.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        视图集ViewSet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="C04-View/Routers.html">
            
                
                    <a href="../C04-View/Routers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        路由Router
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C05-Components/index.html">
            
                
                    <a href="../C05-Components/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        其他功能
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C05-Components/Authentication.html">
            
                
                    <a href="../C05-Components/Authentication.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        认证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C05-Components/Permissions.html">
            
                
                    <a href="../C05-Components/Permissions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        权限
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="C05-Components/Throttling.html">
            
                
                    <a href="../C05-Components/Throttling.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        限流
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="C05-Components/Filtering.html">
            
                
                    <a href="../C05-Components/Filtering.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        过滤
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="C05-Components/Ordering.html">
            
                
                    <a href="../C05-Components/Ordering.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="C05-Components/Pagination.html">
            
                
                    <a href="../C05-Components/Pagination.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="C05-Components/Exceptions.html">
            
                
                    <a href="../C05-Components/Exceptions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        异常处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="C05-Components/Documents.html">
            
                
                    <a href="../C05-Components/Documents.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        自动生成接口文档
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >django项目</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x89C6;&#x56FE;&#x96C6;viewset">&#x89C6;&#x56FE;&#x96C6;ViewSet</h1>
<p>&#x4F7F;&#x7528;&#x89C6;&#x56FE;&#x96C6;ViewSet&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x4E00;&#x7CFB;&#x5217;&#x903B;&#x8F91;&#x76F8;&#x5173;&#x7684;&#x52A8;&#x4F5C;&#x653E;&#x5230;&#x4E00;&#x4E2A;&#x7C7B;&#x4E2D;&#xFF1A;</p>
<ul>
<li>list()  &#x63D0;&#x4F9B;&#x4E00;&#x7EC4;&#x6570;&#x636E;</li>
<li>retrieve()  &#x63D0;&#x4F9B;&#x5355;&#x4E2A;&#x6570;&#x636E;</li>
<li>create()  &#x521B;&#x5EFA;&#x6570;&#x636E;</li>
<li>update()  &#x4FDD;&#x5B58;&#x6570;&#x636E;</li>
<li>destory()  &#x5220;&#x9664;&#x6570;&#x636E;</li>
</ul>
<p>ViewSet&#x89C6;&#x56FE;&#x96C6;&#x7C7B;&#x4E0D;&#x518D;&#x5B9E;&#x73B0;get()&#x3001;post()&#x7B49;&#x65B9;&#x6CD5;&#xFF0C;&#x800C;&#x662F;&#x5B9E;&#x73B0;&#x52A8;&#x4F5C; <strong>action</strong> &#x5982; list() &#x3001;create() &#x7B49;&#x3002;</p>
<p>&#x89C6;&#x56FE;&#x96C6;&#x53EA;&#x5728;&#x4F7F;&#x7528;as_view()&#x65B9;&#x6CD5;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x624D;&#x4F1A;&#x5C06;<strong>action</strong>&#x52A8;&#x4F5C;&#x4E0E;&#x5177;&#x4F53;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#x5BF9;&#x5E94;&#x4E0A;&#x3002;&#x5982;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookInfoViewSet</span><span class="hljs-params">(viewsets.ViewSet)</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">list</span><span class="hljs-params">(self, request)</span>:</span>
        books = BookInfo.objects.all()
        serializer = BookInfoSerializer(books, many=<span class="hljs-keyword">True</span>)
        <span class="hljs-keyword">return</span> Response(serializer.data)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">retrieve</span><span class="hljs-params">(self, request, pk=None)</span>:</span>
        <span class="hljs-keyword">try</span>:
            books = BookInfo.objects.get(id=pk)
        <span class="hljs-keyword">except</span> BookInfo.DoesNotExist:
            <span class="hljs-keyword">return</span> Response(status=status.HTTP_404_NOT_FOUND)
        serializer = BookInfoSerializer(books)
        <span class="hljs-keyword">return</span> Response(serializer.data)
</code></pre>
<p>&#x5728;&#x8BBE;&#x7F6E;&#x8DEF;&#x7531;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5982;&#x4E0B;&#x64CD;&#x4F5C;</p>
<pre><code class="lang-python">urlpatterns = [
    url(<span class="hljs-string">r&apos;^books/$&apos;</span>, BookInfoViewSet.as_view({<span class="hljs-string">&apos;get&apos;</span>:<span class="hljs-string">&apos;list&apos;</span>}),
    url(<span class="hljs-string">r&apos;^books/(?P&lt;pk&gt;\d+)/$&apos;</span>, BookInfoViewSet.as_view({<span class="hljs-string">&apos;get&apos;</span>: <span class="hljs-string">&apos;retrieve&apos;</span>})
]
</code></pre>
<h2 id="1-&#x5E38;&#x7528;&#x89C6;&#x56FE;&#x96C6;&#x7236;&#x7C7B;">1. &#x5E38;&#x7528;&#x89C6;&#x56FE;&#x96C6;&#x7236;&#x7C7B;</h2>
<h4 id="1&#xFF09;-viewset">1&#xFF09; ViewSet</h4>
<p>&#x7EE7;&#x627F;&#x81EA;<code>APIView</code>&#x4E0E;<code>ViewSetMixin</code>&#xFF0C;&#x4F5C;&#x7528;&#x4E5F;&#x4E0E;APIView&#x57FA;&#x672C;&#x7C7B;&#x4F3C;&#xFF0C;&#x63D0;&#x4F9B;&#x4E86;&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1;&#x3001;&#x6743;&#x9650;&#x6821;&#x9A8C;&#x3001;&#x6D41;&#x91CF;&#x7BA1;&#x7406;&#x7B49;&#x3002;</p>
<p><strong>ViewSet&#x4E3B;&#x8981;&#x901A;&#x8FC7;&#x7EE7;&#x627F;ViewSetMixin&#x6765;&#x5B9E;&#x73B0;&#x5728;&#x8C03;&#x7528;as_view()&#x65F6;&#x4F20;&#x5165;&#x5B57;&#x5178;&#xFF08;&#x5982;<code>{&apos;get&apos;:&apos;list&apos;}</code>&#xFF09;&#x7684;&#x6620;&#x5C04;&#x5904;&#x7406;&#x5DE5;&#x4F5C;&#x3002;</strong></p>
<p>&#x5728;ViewSet&#x4E2D;&#xFF0C;&#x6CA1;&#x6709;&#x63D0;&#x4F9B;&#x4EFB;&#x4F55;&#x52A8;&#x4F5C;action&#x65B9;&#x6CD5;&#xFF0C;&#x9700;&#x8981;&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x5B9E;&#x73B0;action&#x65B9;&#x6CD5;&#x3002;</p>
<h4 id="2&#xFF09;genericviewset">2&#xFF09;GenericViewSet</h4>
<p>&#x4F7F;&#x7528;ViewSet&#x901A;&#x5E38;&#x5E76;&#x4E0D;&#x65B9;&#x4FBF;&#xFF0C;&#x56E0;&#x4E3A;list&#x3001;retrieve&#x3001;create&#x3001;update&#x3001;destory&#x7B49;&#x65B9;&#x6CD5;&#x90FD;&#x9700;&#x8981;&#x81EA;&#x5DF1;&#x7F16;&#x5199;&#xFF0C;&#x800C;&#x8FD9;&#x4E9B;&#x65B9;&#x6CD5;&#x4E0E;&#x524D;&#x9762;&#x8BB2;&#x8FC7;&#x7684;Mixin&#x6269;&#x5C55;&#x7C7B;&#x63D0;&#x4F9B;&#x7684;&#x65B9;&#x6CD5;&#x540C;&#x540D;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x7EE7;&#x627F;Mixin&#x6269;&#x5C55;&#x7C7B;&#x6765;&#x590D;&#x7528;&#x8FD9;&#x4E9B;&#x65B9;&#x6CD5;&#x800C;&#x65E0;&#x9700;&#x81EA;&#x5DF1;&#x7F16;&#x5199;&#x3002;&#x4F46;&#x662F;Mixin&#x6269;&#x5C55;&#x7C7B;&#x4F9D;&#x8D56;&#x4E0E;<code>GenericAPIView</code>&#xFF0C;&#x6240;&#x4EE5;&#x8FD8;&#x9700;&#x8981;&#x7EE7;&#x627F;<code>GenericAPIView</code>&#x3002;</p>
<p><strong>GenericViewSet</strong>&#x5C31;&#x5E2E;&#x52A9;&#x6211;&#x4EEC;&#x5B8C;&#x6210;&#x4E86;&#x8FD9;&#x6837;&#x7684;&#x7EE7;&#x627F;&#x5DE5;&#x4F5C;&#xFF0C;&#x7EE7;&#x627F;&#x81EA;<code>GenericAPIView</code>&#x4E0E;<code>ViewSetMixin</code>&#xFF0C;&#x5728;&#x5B9E;&#x73B0;&#x4E86;&#x8C03;&#x7528;as_view()&#x65F6;&#x4F20;&#x5165;&#x5B57;&#x5178;&#xFF08;&#x5982;<code>{&apos;get&apos;:&apos;list&apos;}</code>&#xFF09;&#x7684;&#x6620;&#x5C04;&#x5904;&#x7406;&#x5DE5;&#x4F5C;&#x7684;&#x540C;&#x65F6;&#xFF0C;&#x8FD8;&#x63D0;&#x4F9B;&#x4E86;<code>GenericAPIView</code>&#x63D0;&#x4F9B;&#x7684;&#x57FA;&#x7840;&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x642D;&#x914D;Mixin&#x6269;&#x5C55;&#x7C7B;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x4E3E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> rest_framework <span class="hljs-keyword">import</span> mixins
<span class="hljs-keyword">from</span> rest_framework.viewsets <span class="hljs-keyword">import</span> GenericViewSet
<span class="hljs-keyword">from</span> rest_framework.decorators <span class="hljs-keyword">import</span> action

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookInfoViewSet</span><span class="hljs-params">(mixins.ListModelMixin, mixins.RetrieveModelMixin, GenericViewSet)</span>:</span>
    queryset = BookInfo.objects.all()
    serializer_class = BookInfoSerializer
</code></pre>
<p>url&#x7684;&#x5B9A;&#x4E49;</p>
<pre><code class="lang-python">urlpatterns = [
    url(<span class="hljs-string">r&apos;^books/$&apos;</span>, views.BookInfoViewSet.as_view({<span class="hljs-string">&apos;get&apos;</span>: <span class="hljs-string">&apos;list&apos;</span>})),
    url(<span class="hljs-string">r&apos;^books/(?P&lt;pk&gt;\d+)/$&apos;</span>, views.BookInfoViewSet.as_view({<span class="hljs-string">&apos;get&apos;</span>: <span class="hljs-string">&apos;retrieve&apos;</span>})),
]
</code></pre>
<h4 id="3&#xFF09;modelviewset">3&#xFF09;ModelViewSet</h4>
<p>&#x7EE7;&#x627F;&#x81EA;<code>GenericViewSet</code>&#xFF0C;&#x540C;&#x65F6;&#x5305;&#x62EC;&#x4E86;ListModelMixin&#x3001;RetrieveModelMixin&#x3001;CreateModelMixin&#x3001;UpdateModelMixin&#x3001;DestoryModelMixin&#x3002;</p>
<h4 id="4&#xFF09;readonlymodelviewset">4&#xFF09;ReadOnlyModelViewSet</h4>
<p>&#x7EE7;&#x627F;&#x81EA;<code>GenericViewSet</code>&#xFF0C;&#x540C;&#x65F6;&#x5305;&#x62EC;&#x4E86;ListModelMixin&#x3001;RetrieveModelMixin&#x3002;</p>
<h2 id="2-&#x89C6;&#x56FE;&#x96C6;&#x4E2D;&#x5B9A;&#x4E49;&#x9644;&#x52A0;action&#x52A8;&#x4F5C;">2. &#x89C6;&#x56FE;&#x96C6;&#x4E2D;&#x5B9A;&#x4E49;&#x9644;&#x52A0;action&#x52A8;&#x4F5C;</h2>
<p>&#x5728;&#x89C6;&#x56FE;&#x96C6;&#x4E2D;&#xFF0C;&#x9664;&#x4E86;&#x4E0A;&#x8FF0;&#x9ED8;&#x8BA4;&#x7684;&#x65B9;&#x6CD5;&#x52A8;&#x4F5C;&#x5916;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x6DFB;&#x52A0;&#x81EA;&#x5B9A;&#x4E49;&#x52A8;&#x4F5C;&#x3002;</p>
<p>&#x4E3E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> rest_framework <span class="hljs-keyword">import</span> mixins
<span class="hljs-keyword">from</span> rest_framework.viewsets <span class="hljs-keyword">import</span> GenericViewSet
<span class="hljs-keyword">from</span> rest_framework.decorators <span class="hljs-keyword">import</span> action

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookInfoViewSet</span><span class="hljs-params">(mixins.ListModelMixin, mixins.RetrieveModelMixin, GenericViewSet)</span>:</span>
    queryset = BookInfo.objects.all()
    serializer_class = BookInfoSerializer

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">latest</span><span class="hljs-params">(self, request)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x8FD4;&#x56DE;&#x6700;&#x65B0;&#x7684;&#x56FE;&#x4E66;&#x4FE1;&#x606F;
        &quot;&quot;&quot;</span>
        book = BookInfo.objects.latest(<span class="hljs-string">&apos;id&apos;</span>)
        serializer = self.get_serializer(book)
        <span class="hljs-keyword">return</span> Response(serializer.data)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read</span><span class="hljs-params">(self, request, pk)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x4FEE;&#x6539;&#x56FE;&#x4E66;&#x7684;&#x9605;&#x8BFB;&#x91CF;&#x6570;&#x636E;
        &quot;&quot;&quot;</span>
        book = self.get_object()
        book.bread = request.data.get(<span class="hljs-string">&apos;bread&apos;</span>)
        book.save()
        serializer = self.get_serializer(book)
        <span class="hljs-keyword">return</span> Response(serializer.data)
</code></pre>
<p>url&#x7684;&#x5B9A;&#x4E49;</p>
<pre><code class="lang-python">urlpatterns = [
    url(<span class="hljs-string">r&apos;^books/$&apos;</span>, views.BookInfoViewSet.as_view({<span class="hljs-string">&apos;get&apos;</span>: <span class="hljs-string">&apos;list&apos;</span>})),
    url(<span class="hljs-string">r&apos;^books/latest/$&apos;</span>, views.BookInfoViewSet.as_view({<span class="hljs-string">&apos;get&apos;</span>: <span class="hljs-string">&apos;latest&apos;</span>})),
    url(<span class="hljs-string">r&apos;^books/(?P&lt;pk&gt;\d+)/$&apos;</span>, views.BookInfoViewSet.as_view({<span class="hljs-string">&apos;get&apos;</span>: <span class="hljs-string">&apos;retrieve&apos;</span>})),
    url(<span class="hljs-string">r&apos;^books/(?P&lt;pk&gt;\d+)/read/$&apos;</span>, views.BookInfoViewSet.as_view({<span class="hljs-string">&apos;put&apos;</span>: <span class="hljs-string">&apos;read&apos;</span>})),
]
</code></pre>
<h2 id="3-action&#x5C5E;&#x6027;">3. action&#x5C5E;&#x6027;</h2>
<p>&#x5728;&#x89C6;&#x56FE;&#x96C6;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;action&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#x6765;&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x8BF7;&#x6C42;&#x89C6;&#x56FE;&#x96C6;&#x65F6;&#x7684;action&#x52A8;&#x4F5C;&#x662F;&#x54EA;&#x4E2A;&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_serializer_class</span><span class="hljs-params">(self)</span>:</span>
    <span class="hljs-keyword">if</span> self.action == <span class="hljs-string">&apos;create&apos;</span>:
        <span class="hljs-keyword">return</span> OrderCommitSerializer
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">return</span> OrderDataSerializer
</code></pre>
<h2 id="4-&#x89C6;&#x56FE;&#x96C6;&#x7684;&#x7EE7;&#x627F;&#x5173;&#x7CFB;">4. &#x89C6;&#x56FE;&#x96C6;&#x7684;&#x7EE7;&#x627F;&#x5173;&#x7CFB;</h2>
<p><img src="../images/&#x89C6;&#x56FE;&#x96C6;&#x7C7B;&#x7EE7;&#x627F;&#x5173;&#x7CFB;.png" alt="&#x89C6;&#x56FE;&#x96C6;&#x7684;&#x7EE7;&#x627F;&#x5173;&#x7CFB;"></p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C04-View/ViewIntroduction.html" class="navigation navigation-prev " aria-label="Previous page: 视图说明"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C04-View/Routers.html" class="navigation navigation-next " aria-label="Next page: 路由Router"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontsettings":{"family":"sans","size":2,"theme":"white"},"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book django项目 -->
</html>
